﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GalaxyPresident
{
    

    public class SolarSystem : InvisibleGameEntity
    {
        private string name;
        public static string[] NAMEPRE = new string[] { 
            "Alpha", "Beta","Gamma","Delta","Epsilo","Zeta","Eta",
            "Theta","Iota","Kappa","Lamda","Mu","Nu","Xi","Omicro",
            "Pi","Rho","Sigma","Tau","Upsilo","Phi","Chi","Psi","Omega"
        };
        public static string[] NAMESUF = new string[] {
            "quarius","ces","ries","rus","mini","cer","leo",
            "go","bra","corpio","tarius","ricorn"
        };

        Sun theSun;
        List<Planet> planets;
        Color color;
        Vector3 position;
        float size;

        public float Size
        {
            get { return size; }
            set { size = value; }
        }

        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

  
        public Sun TheSun
        {
            get { return theSun; }
            set { theSun = value; }
        }

        public List<Planet> Planets
        {
            get { return planets; }
            set { planets = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        
        public SolarSystem(GAME_INFO gameInfo, string name ,Vector3 position,float scale)
        {
            Name = name;
            Position = position;
            size = (float)(scale);
            float minSunSize = 100;
            float maxSunSize = 150;
            float sunSize = minSunSize + (maxSunSize - minSunSize) * (float)Constant.random.NextDouble();
            theSun = new Sun(@"Resources/sun", sunSize, 0, new Orbit(true, 0, 0, 0));
            planets = generatePlanetsInSolarSystem(minSunSize, maxSunSize);
            foreach (Planet planet in planets)
            {
                planet.SolarSystem = this;
            }

            foreach (Planet planet in planets)
            {
                planet.Meterial = gameInfo.meterialAmount;
                planet.Energy = gameInfo.energyAmount;
            }            
        }

        private List<Planet> generatePlanetsInSolarSystem(float minSunSize, float maxSunSize)
        {
            float minSize = minSunSize / 2;
            float maxSize = maxSunSize / 2;
            float minVelocity = 0.0006f;
            float maxVelocity = 0.0009f;
            int maxPlanet = 7;
            int minPlanet = 4;

            List<Planet> result = new List<Planet>();

            int numberOfPlanet = (Constant.random.Next(maxPlanet - minPlanet)) + minPlanet;
            int nLivePlanet = 0;
            for (int i = 0; i < numberOfPlanet; i++)
            {
                
                float planetSize = ((float)i/ numberOfPlanet) * minSize*0.4f + 0.8f*minSize +
                    (float)Constant.random.NextDouble() * minSize*0.3f;
                float velocity = minVelocity + (maxVelocity - minVelocity) * (float)Constant.random.NextDouble();
                bool isleft = (Constant.random.Next() % 2 == 0) ? true : false;

                float a = (i * i) + (10 * i + maxSize / 2 + (maxSize) * (i + 1) + 8 * (i + 1) * (float)Constant.random.NextDouble());
                float b = (i * i) + (10 * i + maxSize / 2 + (maxSize) * (i + 1) + 8 * (i + 1) * (float)Constant.random.NextDouble());
                
                float rotatez = (float)(Constant.random.NextDouble() * Math.PI/100);


                if (Constant.random.Next() % 2 == 0 || (i == numberOfPlanet - 1 && nLivePlanet==0))
                {
                    result.Add(new LivalePlanet("Planet "+i.ToString("00"),@"Resources/Ocean", planetSize, velocity, new Orbit(isleft, a, b, rotatez)));
                    nLivePlanet++;
                }
                else
                    result.Add(new DeathPlanet("Planet "+i.ToString("00"),@"Resources/Ocean", planetSize, velocity, new Orbit(isleft, a, b, rotatez)));

                int numberOfMoon = Constant.random.Next(2);
                for (int j = 0; j < numberOfMoon; j++)
                {
                    float moonSize = (float)(planetSize + planetSize * Constant.random.NextDouble()) * 0.2f;
                    
                    float velocityMoon = (minVelocity + (maxVelocity - minVelocity) * (float)Constant.random.NextDouble()) * 5;
                    bool isleftMoon = (Constant.random.Next() % 2 == 0) ? true : false;
                    float aMoon = (planetSize / 2 + (j + 1) * moonSize + 2 * (float)Constant.random.NextDouble()) * (1 + (float)i / 2 / numberOfPlanet);
                    float bMoon = (planetSize / 2 + (j + 1) * moonSize + 2 * (float)Constant.random.NextDouble()) * (1 + (float)i / 2 / numberOfPlanet);
                    float rotateMoon = (float)(Constant.random.NextDouble() * Math.PI / 6);

                    Orbit orbit = new Orbit(isleftMoon, aMoon, bMoon, rotateMoon);
                    orbit.Visible = false;
                    result[i].addSubPlanet(new Moon(@"resources/moon", moonSize, velocityMoon, orbit));
                }
            }
            return result;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            foreach (Planet planet in planets)
            {
                planet.Update(gameTime);
            }
        }

        public override void UpdateInfo(GameTime gameTime)
        {
            base.UpdateInfo(gameTime);
            foreach (Planet planet in planets)
            {
                planet.UpdateInfo(gameTime);
            }
        }
    }
}
